package com.bawei.widgetdemo;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * @Author : yaotianxue
 * @Time : On 2023/6/29 10:46
 * @Description : MyViewGroup
 */
public class MyViewGroup extends ViewGroup {
    public MyViewGroup(Context context) {
        this(context,null);
    }

    public MyViewGroup(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MyViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr,0);
    }

    public MyViewGroup(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //宽度
        int width_mode = MeasureSpec.getMode(widthMeasureSpec);
        int width_size = MeasureSpec.getSize(widthMeasureSpec);

        if(width_mode == MeasureSpec.AT_MOST){//最大值模式:wrap_content
            width_size = 800;
        }
        //高度
        int height_mode = MeasureSpec.getMode(heightMeasureSpec);
        int height_size = MeasureSpec.getSize(heightMeasureSpec);
        if(height_mode == MeasureSpec.AT_MOST){//最大值模式:wrap_content
            height_size = 400;
        }
        setMeasuredDimension(width_size,height_size);


    }
    //摆放子控件的位置
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();//获得子控件的个数
        for (int j= 0;j<childCount;j++){//遍历每个子控件
            View view = getChildAt(j);//获得子控件
            int i = 300;//左
            int i1 = 300;//上
            int i2 = 800;//右
            int i3 = 400;//下
            view.layout(i,i1,i2,i3);//摆放
        }

    }

}
